package org.example;
import java.util.Arrays;
import java.util.Scanner;
public class NGuessNumber {
    public static int a_num=0;
    public static int b_num=0;
    public static int[] numbers=new int[4];
    public static int[] guesslist=new int[4];
    public static int[] generateAnswer(){
        //随机生成4个0-9之间的不重复的整型数作为答案并返回的功能。
        int index=0;
        while(true){
            int random=(int)(Math.random()*9+1);
            int j=0;
            for(;j<numbers.length;j++){
                if(random==numbers[j]){
                    break;}}
            if(j==numbers.length){numbers[index++]=random;}
            if(index==4){
                break;
            }}
        return numbers;}
    public static int[] getPlayerInput(){
        //接收玩家输入的4个数字并返回。
        for(int a=0;a<4;a++){
            guesslist[a]=a;//默认值0123
        }
        System.out.println(Arrays.toString(guesslist));
        try {
            Scanner sca=new Scanner(System.in);
            for(int i=0;i<4;i++){
                System.out.println("Please enter the "+ (i+1) +" number between 0-9:");
                int pl=sca.nextInt();
                if(pl>=10||pl<0){
                    System.out.print("input error");
                    i=i-1;
                }else{
                    guesslist[i] = pl;
                }
            }
        }catch(Exception e){
            System.out.println("input error");
        }
        System.out.println(Arrays.toString(guesslist)+"\n");
        return guesslist;
    }
    public static String compareGuessAnswer(){
        a_num=0;
        b_num=0;
        //比较玩家猜测和答案后返回提示信息。
        //A 存在且位置正确，B 存在且位置不正确 1A1B 1表示数量
        for(int i=0;i<numbers.length;i++) {
            for(int l=0;l<numbers.length;l++){
                if(i!=l){
                    if(numbers[i]==guesslist[l]){
                        b_num++;
                    }
                }else{
                    if(numbers[i]==guesslist[l]){
                        a_num++;
                    }}}}
        System.out.println(a_num+"A"+b_num+"B");
        System.out.println("A represents the number of digits that exist and are in the correct position,and B represents the number of digits that exist and are not in the correct position!");
        System.out.println("E.g.:1A2B,it means that 3 of the four numbers you guessed are correct,one position is correct,and two positions are incorrect.\n");
        return a_num+"A"+b_num+"B\n"+"A represents the number of digits that exist and are in the correct position,and B represents the number of digits that exist and are not in the correct position!\n"+"E.g.:1A2B,it means that 3 of the four numbers you guessed are correct,one position is correct,and two positions are incorrect.";
    }
    public static boolean isWin(){
        boolean flag=false;
        //根据提示信息中A的数量判断玩家是否获胜，如果获胜返回true，否则返回false。
        if(a_num==4){
            flag=true;
        }else{
            flag=false;
        }
        return flag;
    }
    public static void main(String[] args) {
        System.out.println("|----New Number Guessing Game-----|");
        while(true){
            System.out.println("|----Enter 1 to start the game----|");
            System.out.println("|----Enter 0 to end the game----|\n");
            Scanner playerinput=new Scanner(System.in);
            switch(playerinput.nextInt()){
                case 0:
                    System.out.println("----Exited the game----\n");
                    System.exit(0);
                    break;
                case 1:
                    int cs=0;
                    int sy=7;
                    System.out.println("----The game has started(limited to 7 times)----\n");
                    generateAnswer();
                    while(true){
                        getPlayerInput();
                        compareGuessAnswer();
                        if(isWin()==true){
                            System.out.println("Player Wins\n");
                            break;
                        }else{
                            cs++;
                            sy--;
                            if(cs==7){
                                System.out.println("7 times,the correct answer is" + Arrays.toString(numbers));
                                System.out.println("|----Another round?------|");
                                break;
                            }System.out.println("Guess wrong,come again!(" + sy + "times left)\n");
                        }}
                    break;
                    default:
                    System.out.println("Incorrect character entered,please enter 0/1\n");}}}}